---
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Copy network configuration
  template:
    src: "suse_interface_default.cfg.j2"
    dest: "/etc/sysconfig/network/ifcfg-{{ item.name | default('br-mgmt') }}"
  with_items: "{{ bridges }}"
  register: network_interfaces

- name: Put down post-up script for veth-peer interfaces
  template:
    src: "rpm_interface_{{ item[0] }}.cfg.j2"
    dest: "/etc/sysconfig/network/scripts/{{ item[0] }}-veth-{{ item[1].name | default('br-mgmt') }}-2-{{ item[1].veth_peer | default('eth1') }}"
    mode: "0755"
  with_nested:
    - [ "ifup-post", "ifdown-post" ]
    - "{{ bridges }}"
  when:
    - item[1].veth_peer is defined

- name: Ensure the postup scripts are loaded
  lineinfile:
    dest: "/etc/sysconfig/network/ifcfg-{{ item[1].name | default('br-mgmt') }}"
    line: "POST_UP_SCRIPT=\"compat:suse:{{ item[0] }}-veth-{{ item[1].name | default('br-mgmt') }}-2-{{ item[1].veth_peer | default('eth1') }}\""
  with_nested:
    - [ "ifup-post" ]
    - "{{ bridges }}"
  when:
    - item[1].veth_peer is defined

- name: Ensure the postdown scripts are loaded
  lineinfile:
    dest: "/etc/sysconfig/network/ifcfg-{{ item[1].name | default('br-mgmt') }}"
    line: "POST_DOWN_SCRIPT=\"compat:suse:{{ item[0] }}-veth-{{ item[1].name | default('br-mgmt') }}-2-{{ item[1].veth_peer | default('eth1') }}\""
  with_nested:
    - [ "ifdown-post" ]
    - "{{ bridges }}"
  when:
    - item[1].veth_peer is defined
